package com.hrm.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hrm.dao.IconMapper;
import com.hrm.entity.Icon;
import com.hrm.service.IconService;

public class IconServiceImpl implements IconService {
	private static final Log log = LogFactory.getLog(IconServiceImpl.class); 
	@Autowired
	private IconMapper iconMapper;
	public IconMapper getIconMapper() {
		return iconMapper;
	}

	public void setIconMapper(IconMapper iconMapper) {
		this.iconMapper = iconMapper;
	}

	public List<Icon> getIcons() {
		log.info("****** Do method getIcons() begin,no params info.");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("parentId", -1);
		log.info("set parentId=-1 to get level 1 icons.");
		List<Icon> icons=iconMapper.selectIconByMap(map);
//		for(Icon icon :icons){
//			icon.setIcons(this.getIconsByParentId(icon.getId()));
//		}
		log.info("****** Do method getRoles() end,return info ==== [ List<Icon>="+icons+" ]");
		return icons;
	}
	public List<Icon> getAllParentIcons() {
		log.info("****** Do method getAllParentIcons() begin,no params info.");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("parentId", -1);
		List<Icon> icons=iconMapper.selectIconByMap(map);
		log.info("****** Do method getAllParentIcons() end,return info ==== [ List<Icon>="+icons+" ]");
		return icons;
	}
	public Icon getIconById(long id) {
		log.info("****** Do method getIconById(long id) begin,params info ==== [ id="+id+" ]");
		Icon icon =  iconMapper.selectIconById(id);
		log.info("****** Do method getIconById(long id) end,return info ==== [ Icon="+icon+" ]");
		return icon;
	}

	public List<Icon> getIconsByParentId(long parentId) {
		log.info("****** Do method getIconsByParentId(long parentId) begin,params info ==== [ parentId="+parentId+" ]");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("parentId", parentId);
		List<Icon> icons = iconMapper.selectIconByMap(map);
		log.info("****** Do method getIconsByParentId(long parentId) end,return info ==== [ List<Icon>="+icons+" ]");
		return icons;
	}

	public Icon getIconByPath(String path) {
		log.info("****** Do method getIconByPath(String path) begin,params info ==== [ path="+path+" ]");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("path", path);
		List<Icon> icons = iconMapper.selectIconByMap(map);
		if(icons==null){
			log.info("****** Do method getIconByPath(String path) end,return info ==== [ Icon=null ]");
		}
		log.info("****** Do method getIconByPath(String path) end,return info ==== [ Icon="+icons.get(0)+" ]");
		return icons.get(0);
	}

	public int addIcon(Icon icon) {
		log.info("****** Do method addIcon(Icon icon) begin,params info ==== [ icon="+icon+" ]");
		int cnts=-1;
		cnts= iconMapper.insertIcon(icon);
		log.info("****** Do method addIcon(Icon icon) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int updateIcon(Icon icon) {
		log.info("****** Do method updateIcon(Icon icon) begin,params info ==== [ icon="+icon+" ]");
		int cnts=-1;
		cnts= iconMapper.updateIcon(icon);
		log.info("****** Do method updateIcon(Icon icon) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}

	public int deleteIconById(long id) {
		log.info("****** Do method deleteIconById(long id) begin,params info ==== [ id="+id+" ]");
		Icon icon = this.getIconById(id);
		String tmp = this.getClass().getResource("/").getPath();
		String imagePath=tmp.substring(0,tmp.indexOf("WEB-INF"))+icon.getPath();
		File file = new File(imagePath); //新建一个文件
		try {
			 file.delete(); //删除文件
			 log.info("delete icon file ["+imagePath+"] success!");
		} catch (Exception e) {
			log.error("delete icon file ["+imagePath+"] failed!");
			e.printStackTrace();
			return -1;
		}
		int cnts=-1;
		cnts= iconMapper.deleteIconById(id);
		log.info("****** Do method deleteIconById(long id) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}
	public int deleteIconsByParentId(long parentId) {
		log.info("****** Do method deleteIconsByParentId(long parentId) begin,params info ==== [ parentId="+parentId+" ]");
		List<Icon> icons = this.getIconsByParentId(parentId);
//		String tmp = this.getClass().getResource("/").getPath();
		for (Icon icon : icons) {
			this.deleteIconById(icon.getId());
//			String imagePath=tmp.substring(0,tmp.indexOf("WEB-INF"))+icon.getPath();
//			File file = new File(imagePath); //新建一个文件
//			try {
//				file.delete(); //删除文件
//			} catch (Exception e) {
//				e.printStackTrace();
//				return -1;
//			}
//			iconMapper.deleteIconById(icon.getId());
		}
		int cnts=-1;
		cnts= this.deleteIconById(parentId);
		log.info("****** Do method deleteIconsByParentId(long parentId) end,return info ==== [ int="+cnts+" ]");
		return cnts;
	}


}
